package com.back.service.impl;

import com.back.dto.CompanyVerificationDTO;
import com.back.entity.Company;
import com.back.entity.CompanyVerificationLog;
import com.back.mapper.CompanyMapper;
import com.back.mapper.CompanyVerificationLogMapper;
import com.back.service.ICompanyVerificationLogService;
import com.back.vo.CompanyVerificationLogVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  企业认证记录服务实现类
 * </p>
 *
 * @author liyang
 * @since 2025-04-26
 */
@Service
public class CompanyVerificationLogServiceImpl extends ServiceImpl<CompanyVerificationLogMapper, CompanyVerificationLog> implements ICompanyVerificationLogService {

    @Autowired
    private CompanyMapper companyMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CompanyVerificationLogVO submitVerification(Long companyId, CompanyVerificationDTO verificationDTO) {
        // 更新企业营业执照
        Company company = companyMapper.selectById(companyId);
        if (company == null || company.getDeleted()) {
            return null;
        }
        
        company.setBusinessLicense(verificationDTO.getBusinessLicense());
        company.setVerificationStatus("pending"); // 设置为待审核状态
        company.setUpdateTime(System.currentTimeMillis());
        companyMapper.updateById(company);
        
        // 创建认证记录
        CompanyVerificationLog verificationLog = new CompanyVerificationLog();
        verificationLog.setCompanyId(companyId);
        verificationLog.setStatus("pending");
        long currentTime = System.currentTimeMillis();
        verificationLog.setCreateTime(currentTime);
        verificationLog.setUpdateTime(currentTime);
        verificationLog.setDeleted(false);
        
        save(verificationLog);
        
        // 转换为VO返回
        CompanyVerificationLogVO logVO = new CompanyVerificationLogVO();
        BeanUtils.copyProperties(verificationLog, logVO);
        
        return logVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CompanyVerificationLogVO reviewVerification(Long id, Long adminId, String status, String reason) {
        // 查询认证记录
        CompanyVerificationLog verificationLog = getById(id);
        if (verificationLog == null || verificationLog.getDeleted()) {
            return null;
        }
        
        // 更新认证记录
        verificationLog.setAdminId(adminId);
        verificationLog.setStatus(status);
        verificationLog.setReason(reason);
        verificationLog.setUpdateTime(System.currentTimeMillis());
        updateById(verificationLog);
        
        // 更新企业认证状态
        Company company = companyMapper.selectById(verificationLog.getCompanyId());
        if (company != null && !company.getDeleted()) {
            company.setVerificationStatus(status);
            company.setUpdateTime(System.currentTimeMillis());
            companyMapper.updateById(company);
        }
        
        // 转换为VO返回
        CompanyVerificationLogVO logVO = new CompanyVerificationLogVO();
        BeanUtils.copyProperties(verificationLog, logVO);
        
        return logVO;
    }

    @Override
    public IPage<CompanyVerificationLogVO> getVerificationLogList(Page<CompanyVerificationLog> page, String status) {
        // 构建查询条件
        LambdaQueryWrapper<CompanyVerificationLog> queryWrapper = new LambdaQueryWrapper<>();
        
        // 状态筛选
        if (status != null) {
            queryWrapper.eq(CompanyVerificationLog::getStatus, status);
        }
        
        queryWrapper.eq(CompanyVerificationLog::getDeleted, false)
                    .orderByDesc(CompanyVerificationLog::getCreateTime);
        
        // 执行分页查询
        Page<CompanyVerificationLog> logPage = page(page, queryWrapper);
        
        // 转换为VO
        Page<CompanyVerificationLogVO> resultPage = new Page<>();
        BeanUtils.copyProperties(logPage, resultPage, "records");
        
        // 转换记录
        List<CompanyVerificationLogVO> logVOList = logPage.getRecords().stream().map(log -> {
            CompanyVerificationLogVO logVO = new CompanyVerificationLogVO();
            BeanUtils.copyProperties(log, logVO);
            return logVO;
        }).collect(Collectors.toList());
        
        resultPage.setRecords(logVOList);
        
        return resultPage;
    }

    @Override
    public CompanyVerificationLogVO getVerificationLogDetail(Long id) {
        // 查询认证记录
        CompanyVerificationLog verificationLog = getById(id);
        if (verificationLog == null || verificationLog.getDeleted()) {
            return null;
        }
        
        // 转换为VO
        CompanyVerificationLogVO logVO = new CompanyVerificationLogVO();
        BeanUtils.copyProperties(verificationLog, logVO);
        
        return logVO;
    }

    @Override
    public List<CompanyVerificationLogVO> getCompanyVerificationHistory(Long companyId) {
        // 构建查询条件
        LambdaQueryWrapper<CompanyVerificationLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CompanyVerificationLog::getCompanyId, companyId)
                    .eq(CompanyVerificationLog::getDeleted, false)
                    .orderByDesc(CompanyVerificationLog::getCreateTime);
        
        // 执行查询
        List<CompanyVerificationLog> logList = list(queryWrapper);
        
        // 转换为VO列表
        return logList.stream().map(log -> {
            CompanyVerificationLogVO logVO = new CompanyVerificationLogVO();
            BeanUtils.copyProperties(log, logVO);
            return logVO;
        }).collect(Collectors.toList());
    }
}
